// pages/article/activity/index.js
const {
  getActivityConfig,
  couponUseList,
  payMember
} = require('../../../api/api')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    activityId: null,
    activityDetail: {},
    bottomLift: getApp().globalData.bottomLift,
    loginVisible: true,
    visible: false,
    title: '可用优惠券',
    content: '你有一张优惠券可用',
    show:false,
    hideButton:true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if (options.id) {
      this.setData({
        activityId: options.id,
        userInfo:wx.getStorageSync('userInfo')
      })
      this.loadData()
    }
    this.fetchUserInfo()
  },
  async fetchUserInfo() {
    const {vipType}= await getApp().fetchUserInfo()
    this.setData({
      hideButton:vipType!==''?true:false,
   }) 
  },
  async loadData() {
    const {
      result
    } = await getActivityConfig(this.data.activityId)
    this.setData({
      activityDetail: result
    })
    wx.setNavigationBarTitle({
      title: result.title,
    })

  },
  confirm(e) {
    const couponId = e.detail
    this.doWxPay(couponId)
  },
  async pay() {
    const {
      netPrice,vipType
    } = this.data.activityDetail
    if (!wx.getStorageSync('subjectInfo')) {
      this.setData({
        show: true
      })
      return
    }
    const {
      result
    } = await couponUseList(netPrice)
    if (result.length > 0) {
      this.setData({
        visible: true,
        couponList: result,
        content: '您有' + result.length + '张优惠券可用',
        vipType: type
      })
    } else {
      this.doWxPay()
    }

  },
  async update() {
    const {
      netPrice
    } = this.data.activityDetail
    const {
      result
    } = await couponUseList(netPrice)
    if (result.length > 0) {
      this.setData({
        visible: true,
        couponList: result,
        content: '您有' + result.length + '张优惠券可用',
      })
    } else {
      this.doWxPay()
    }
  },
  async doWxPay(couponId) {
    const {activityDetail,activityId}=this.data
    const {
      score,
      ranking,
      year,
      firstSelectSubjects,
      secondSelectSubjects,
      provinceId,
      province
    } = wx.getStorageSync('subjectInfo')
    const params = {
      provinceId,
      province,
      score,
      ranking,
      firstSelect: firstSelectSubjects,
      secondSelect: secondSelectSubjects,
      year,
      vipType: activityDetail.vipType,
      activityPriceId:activityId
    }
    if (couponId) {
      params.couponId = couponId
    }
    const {
      result
    } = await payMember(params)
    const _this = this
    wx.requestPayment({
      nonceStr: result.nonceStr,
      package: result.package,
      paySign: result.paySign,
      timeStamp: result.timeStamp,
      signType: result.signType,
      success: async function (res) {
        wx.showToast({
          icon: 'success',
          title: '支付完成',
        })
        getApp().fetchUserInfo()
        setTimeout(() => {
          wx.navigateBack({
            delta: 1
          })
        }, 300);
      },
      fail: function (res) {
        wx.showToast({
          icon: 'none',
          title: '取消支付',
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    return {
      title: this.data.activityDetail.title,
      path: `/pages/article/activity/index?id=` + this.data.activityId,
      imageUrl:'https://ehsimg.oss-cn-beijing.aliyuncs.com/temp/06777ef4a5f94f08910b2b2dd06c3e57.png',
    }
  }
})